namespace Sharpmark.PerfectLife.Core.Picture.Utilities.ImageToText
{
    using System;
    using System.Drawing;
    using System.Text;

    public class PhotoHtmlText : BaseText
    {
        public Font Font { get; set; }
        public bool EnableFontBackColor { get; set; }
        public char ReplaceChar { get; set; }

        public PhotoHtmlText(Photo photo, int charsWidth, int charsHeight, char replaceStr, Font font, bool enablebg)
            : base(photo, charsWidth, charsHeight)
        {
            ReplaceChar = replaceStr;
            Font = font;
            EnableFontBackColor = enablebg;
        }

        public PhotoHtmlText(Photo photo, int charsWidth, int charsHeight, char replaceStr, bool enablebg)
            : this(photo, charsWidth, charsHeight, replaceStr, new Font(FontFamily.GenericMonospace, 12), enablebg)
        {
        }

        public PhotoHtmlText(Photo photo, int charsWidth, int charsHeight)
            : this(photo, charsWidth, charsHeight, ReplaceCharsList.Chars1[0], false)
        {
        }

        public PhotoHtmlText(Photo photo)
            : this(photo, photo.Width, photo.Height)
        {
        }

        public PhotoHtmlText(System.Drawing.Bitmap image)
            : this(new Photo(image))
        {
        }

        public override string GenerateText()
        {
            StringBuilder sbHtml = new StringBuilder();

            // html format
            sbHtml.Append("<html>\n");
            sbHtml.Append("<body>\n");
            sbHtml.AppendFormat("<font face = \"{0}\" size = \"1\"><div>\n",
                Font.Name, (Font.Size <= 7) ? Int32.Parse(Math.Round(Font.Size).ToString()) : 3);

            if (Font.Italic)
                sbHtml.Append("<EM>");
            if (Font.Strikeout)
                sbHtml.Append("<STRIKE>");
            if (Font.Underline)
                sbHtml.Append("<U>");
            if (Font.Bold)
                sbHtml.Append("<STRONG>");

            //NOTE:从基类的GenerateText()调用子类的ConvertColorToString()
            sbHtml.Append(base.GenerateText());

            //NOTE:注意顺序
            if (Font.Bold)
                sbHtml.Append("</STRONG>");
            if (Font.Underline)
                sbHtml.Append("</U>");
            if (Font.Strikeout)
                sbHtml.Append("</STRIKE>");
            if (Font.Italic)
                sbHtml.Append("</EM>");

            sbHtml.Append("</div></font></body></html>");

            return sbHtml.ToString();
        }

        protected override string ConvertPixelToString(System.Drawing.Color color)
        {
            StringBuilder htmlBuilder = new StringBuilder();

            htmlBuilder.Append("<font ");
            if (EnableFontBackColor)
            {
                htmlBuilder.Append("style=\"BACKGROUND-COLOR: #" + color.ToArgb().ToString("X").Substring(2, 6) + "\"");
            }

            htmlBuilder.Append(" color = \"#" + color.ToArgb().ToString("X").Substring(2, 6) + "\">" + ReplaceChar + "</font>");

            return htmlBuilder.ToString();
        }

        protected override string NewLine()
        {
            //return "<br />";
            return "</div><div>";
        }
    }
}